package com.hzncc.scaffold.controller.Rear.teachingClassroom.QA;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Ask;
import com.hzncc.scaffold.entity.Other.Reply;
import com.hzncc.scaffold.entity.Vo.AskIdVo;
import com.hzncc.scaffold.service.AskService;
import com.hzncc.scaffold.service.ReplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: DrunkBear
 * @Date: 2022-05-12 22:48
 * @Description: null
 */
@Api(tags = "教学课堂管理-问答管理-问题管理")
@RequestMapping("/manage/ask")
@RestController
public class AskManagementController {

    @Autowired
    AskService askService;

    @Autowired
    ReplyService replyService;


    @ApiOperation("查询问题列表")
    @GetMapping("/list")
    public ResultT list(Integer pageNum, Integer pageSize, String keyword, Integer courseId, Integer status) {
        ResultT resultT = askService.list(pageNum, pageSize, keyword, courseId, status);
        return resultT;
    }

    @ApiOperation("/根据编号删除问题")
    @PostMapping("/delete")
    public ResultT delete(@RequestBody AskIdVo askIdVo) {

        Integer askId = askIdVo.getAskId();

        boolean remove = askService.removeById(askId);
        if (remove) {
            return ResultT.success("删除成功");
        } else {
            return ResultT.fail("删除失败");
        }
    }

    @PostMapping("/refuse")
    @ApiOperation("根据编号不通过问题")
    public ResultT refuse(@RequestBody AskIdVo askIdVo) {

        Integer askId = askIdVo.getAskId();

        if (askService.getById(askId) != null) {
            Ask ask = askService.getById(askId);
            ask.setStatus(2);
            boolean update = askService.updateById(ask);
            if (update) {
                return ResultT.success("操作成功");
            } else {
                return ResultT.fail("操作失败");
            }
        } else {
            return ResultT.fail("数据错误");
        }
    }

    @PostMapping("/publish")
    @ApiOperation("根据编号通过问题")
    public ResultT publish(@RequestBody AskIdVo askIdVo) {

        Integer askId = askIdVo.getAskId();

        if (askService.getById(askId) != null) {
            Ask ask = askService.getById(askId);
            ask.setStatus(1);
            ask.setPubTime(DateUtil.now());

            boolean update = askService.updateById(ask);
            if (update) {
                LambdaQueryWrapper<Reply> lqw = new LambdaQueryWrapper<>();
                lqw.eq(Reply::getAskId,askId);
                lqw.eq(Reply::getStatus,1);
                List<Reply> replyList = replyService.list(lqw);
                ask.setReplyNum(replyList.size());
                ask.setLastReplyTime(DateUtil.now());
                askService.updateById(ask);

                return ResultT.success("操作成功");
            } else {
                return ResultT.fail("操作失败");
            }
        } else {
            return ResultT.fail("数据错误");
        }
    }
}
